// course_manager.c
#include "../include/course_manager.h"
#include <stdio.h>
#include <string.h>

#define MAX_COURSES 100
#define CLASS_FILE "/home/chp/桌面/MyProject/txt_File/class.txt"

Course courses[MAX_COURSES];
int course_count = 0;

// 从文件加载课程信息
void load_courses() {
    FILE *file = fopen(CLASS_FILE, "r");
    if (file != NULL) {
        while (fscanf(file, "%s %s %s %d %d %d %d", courses[course_count].course_id, courses[course_count].name, courses[course_count].nature, &courses[course_count].hours, &courses[course_count].credits, &courses[course_count].enrolled_students, &courses[course_count].max_students) != EOF) {
            course_count++;
        }
        fclose(file);
    }
}

// 保存课程信息到文件
void save_courses() {
    FILE *file = fopen(CLASS_FILE, "w");
    for (int i = 0; i < course_count; i++) {
        fprintf(file, "%s %s %s %d %d %d %d\n", courses[i].course_id, courses[i].name, courses[i].nature, courses[i].hours, courses[i].credits, courses[i].enrolled_students, courses[i].max_students);
    }
    fclose(file);
}


// 新增课程
void add_course(const char* course_id, const char* course_name, const char* nature, int hours, int credits, int max_students) {
    if (course_count < MAX_COURSES) {
        strcpy(courses[course_count].course_id, course_id);
        strcpy(courses[course_count].name, course_name);
        strcpy(courses[course_count].nature, nature);
        courses[course_count].hours = hours;
        courses[course_count].credits = credits;
        courses[course_count].max_students = max_students;
        courses[course_count].enrolled_students = 0;
        course_count++;
        save_courses();
        log_operation("add_course", course_id);
    }
}

// 选课
void enroll_student_in_course(const char* course_id) {
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].course_id, course_id) == 0) {
            if (courses[i].enrolled_students < courses[i].max_students) {
                courses[i].enrolled_students++;
                // 记录选课操作的日志
                log_operation("Enroll", course_id);
                save_courses(); // 确保在这里保存课程信息
                return; // 成功选课，返回
            } else {
                // 课程已满，不修改课程信息，打印消息并返回
                printf("课程已满，无法选课: %s\n", course_id);
                return;
            }
        }
    }
    // 未找到课程，打印消息
    printf("未找到课程: %s\n", course_id);
}

//减少选课人数的函数
void decrease_enrolled_students(const char* course_id) {
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].course_id, course_id) == 0) {
            if (courses[i].enrolled_students > 0) {
                courses[i].enrolled_students--;
                save_courses(); // 确保在这里保存课程信息
            }
            return;
        }
    }
    printf("未找到课程: %s\n", course_id);
}



// 删除课程
void delete_course(const char* course_id) {
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].course_id, course_id) == 0) {
            for (int j = i; j < course_count - 1; j++) {
                courses[j] = courses[j + 1];
            }
            course_count--;
            save_courses();
            log_operation("delete_course", course_id);
            break;
        }
    }
}

// 更新课程信息
void update_course(const char* course_id, int hours, int credits, int max_students) {
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].course_id, course_id) == 0) {
            courses[i].hours = hours;
            courses[i].credits = credits;
            courses[i].max_students = max_students;
            save_courses();
            log_operation("update_course", course_id);
            break;
        }
    }
}

// 列出所有课程ID和名称
void list_courses() {
    printf("课程ID\t课程名称\n");
    for (int i = 0; i < course_count; i++) {
        printf("%s\t%s\n", courses[i].course_id, courses[i].name);
    }
}


// 查看课程信息
void view_course(const char* course_id) {
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].course_id, course_id) == 0) {
            printf("课程ID: %s\n课程名称: %s\n课程性质: %s\n授课学时: %d\n学分: %d\n人数上限: %d\n课程已选人数: %d\n",
                   courses[i].course_id, courses[i].name, courses[i].nature, courses[i].hours, courses[i].credits, courses[i].max_students, courses[i].enrolled_students);
            return;
        }
    }
    printf("未找到课程: %s\n", course_id);
}
